//
//  HomeXieYView.m
//  NtoccDriverNew
//
//  Created by 张天兴 on 2023/6/6.
//  Copyright © 2023 GCY. All rights reserved.
//

#import "HomeXieYView.h"


@implementation HomeXieYView

- (instancetype)initWithFrame:(CGRect)frame Url:(NSString *)url{
    
    self = [super initWithFrame:frame];
    if (self) {
//        self._urlString = @"http://36.137.133.24:8000/driver.html";
        self._urlString = url;

        self.isUrlloaded = NO;
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI{
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.contentView];
    self.webView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.comitButton];

    if (@available(iOS 11.0, *)) {
        self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
//    [self.webView addSubview:self.fontSlider];
    
//    [self.contentView addSubview:self.progressLine];
}
- (UIView *)bgView{
    if(!_bgView){
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
        self.bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    }
    return _bgView;
}
- (UIView *)contentView{
    if(!_contentView){
        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(40, 150, ScreenW - 80, ScreenH - 300)];
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.contentView.layer.masksToBounds = YES;
        self.contentView.layer.cornerRadius = 10;
    }
    return _contentView;
}
- (UIButton *)comitButton{
    if(!_comitButton){
        self.comitButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.comitButton setTitle:@"已知晓" forState:UIControlStateNormal];
        [self.comitButton setBackgroundColor:Line_TextColor];
        cut(self.comitButton, BTN_BORDER_RADIUS);
        self.comitButton.tag = 301;
        [self.comitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.comitButton.userInteractionEnabled = NO;
        self.comitButton.frame = CGRectMake(0,ScreenH - 300 -45 , ScreenW - 80, 45);
    }
    return _comitButton;
}
- (WebviewProgressLine *)progressLine {
    if (!_progressLine) {
        _progressLine = [[WebviewProgressLine alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 2)];
        _progressLine.lineColor = Theme_ColorValue;
    }
    return _progressLine;
}
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    DebugLog(@"开始加载 = %@", self._urlString);
    [_progressLine startLoadingAnimation];
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    DebugLog(@"加载完成");
    
    [_progressLine endLoadingAnimation];
    self.isUrlloaded = YES;
    
    //禁止用户选择
    //    [webView evaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none';" completionHandler:nil];
    //    [webView evaluateJavaScript:@"document.activeElement.blur();" completionHandler:nil];
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
    DebugLog(@"加载失败");
    [_progressLine endLoadingAnimation];
}

- (WKWebView *)webView{
    if (!_webView) {
        //适配文字大小
        NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
        WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *wkUController = [[WKUserContentController alloc] init];
        [wkUController addUserScript:wkUScript];
        
        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
        wkWebConfig.userContentController = wkUController;
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenW - 80, ScreenH - NavHeight -70 -150) configuration:wkWebConfig];
        _webView.UIDelegate = self;
        _webView.navigationDelegate = self;
        _webView.scrollView.delegate = self;
            
        NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)self._urlString,(CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",NULL,kCFStringEncodingUTF8));
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:encodedString]]];
//            [self createXiYiView];
            
        [self.contentView addSubview:_webView];
        
    }
    return _webView;
}
#pragma mark - 代收协议相关
- (void)createXiYiView {
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - NavHeight - 65, ScreenW, 65)];
    view.backgroundColor = [UIColor whiteColor];
    [self addSubview:view];
    
    CGFloat width = (ScreenW-40-40)/2;
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setBackgroundColor:[UIColor whiteColor]];
    cut(cancelButton, BTN_BORDER_RADIUS);
    cutBorder(cancelButton, 1, @"#2D7DF6");
    cancelButton.tag = 300;
    [cancelButton addTarget:self action:@selector(xiYiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton setTitleColor:Theme_ColorValue forState:UIControlStateNormal];
    cancelButton.frame = CGRectMake(20, 10, width, 45);
    [view addSubview:cancelButton];
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"同意" forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:Theme_ColorValue];
    cut(confirmButton, BTN_BORDER_RADIUS);
    confirmButton.tag = 301;
    [confirmButton addTarget:self action:@selector(xiYiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmButton.frame = CGRectMake(60 + width, 10, width, 45);
    [view addSubview:confirmButton];
}

- (void)xiYiBtnClick:(UIButton *)button {
//    if (self.XieYiBlock) {
//        self.XieYiBlock(button.tag == 300 ? 0 : 1);
//        [self.navigationController popViewControllerAnimated:YES];
//    }
}
#pragma mark - ASValueTrackingSliderDataSource

- (NSString *)slider:(ASValueTrackingSlider *)slider stringForValue:(float)value;
{
    value = roundf(value);
    
    [_webView evaluateJavaScript:[NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%@%@'", [NSString stringWithFormat:@"%f", value * 50], @"%"] completionHandler:nil];
    
    NSString *s;
    if (value < 2.0) {
        s = @"字体太小";
    } else if (value > 2.0 && value < 5.0) {
        s = @"字体适中";
    } else if (value >= 6.0) {
        s = @"字体太大啦";
    }
    return s;
}


- (ASValueTrackingSlider *)fontSlider {
    if (!_fontSlider) {
        
        _fontSlider =  [[ASValueTrackingSlider alloc] initWithFrame:CGRectMake(0, self.webView.bottom - SY(40), SX(200), SY(30))];
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        [formatter setNumberStyle:NSNumberFormatterPercentStyle];
        [_fontSlider setNumberFormatter:formatter];
        _fontSlider.font = [UIFont fontWithName:@"Futura-CondensedExtraBold" size:26];
        _fontSlider.minimumValue = 1.0;
        _fontSlider.maximumValue = 10.0;
        _fontSlider.popUpViewCornerRadius = 16.0;
        _fontSlider.dataSource = self;
        _fontSlider.centerX = _webView.centerX;
        
        _fontSlider.maximumTrackTintColor = Theme_ColorValue;//大于滑块当前值滑块条的颜色
//        _fontSlider.thumbTintColor = [UIColor yellowColor];//当前滑块颜色
        
        UIColor *blue = [UIColor colorWithHue:0.55 saturation:0.75 brightness:1.0 alpha:1.0];
        UIColor *green = [UIColor colorWithHue:0.3 saturation:0.65 brightness:0.8 alpha:1.0];
        UIColor *yellow = [UIColor colorWithHue:0.15 saturation:0.9 brightness:0.9 alpha:1.0];
        UIColor *red = [UIColor colorWithHue:0.0 saturation:0.8 brightness:1.0 alpha:1.0];
        
        [_fontSlider setPopUpViewAnimatedColors:@[blue, green, yellow, red]
                                  withPositions:@[@0, @2, @5, @6]];
//        _fontSlider.transform = CGAffineTransformMakeRotation(M_PI_2);

    }
    return _fontSlider;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(self.isUrlloaded){
        if(scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)){
            self.comitButton.userInteractionEnabled = YES;
            self.comitButton.backgroundColor = Theme_ColorValue;
        }else if(scrollView.contentOffset.y < scrollView.contentSize.height){
            
        }
    }
}

@end
